package mes.kanban.controller;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import mes.kanban.entity.MesRoleAndJob;
import mes.kanban.entity.MesRoleAndPermission;
import mes.kanban.entity.MesUserAndRole;
import mes.kanban.service.MesRoleServiceI;
import mes.kanban.util.PageBean;
import mes.kanban.vo.AjaxReturnVO;
import mes.kanban.vo.MesRoleAndPermissionVO;
import mes.kanban.vo.MesRoleVO;
import mes.kanban.vo.MesUserAndRoleVO;


/**
 * 用户角色管理
 * @author zongrui.liu
 *
 */
@Controller
@RequestMapping(value="/userRole")
public class UserRoleController implements Serializable{

	private static final long serialVersionUID = 2131046413561732053L;
	
	private static final Logger log = LoggerFactory.getLogger(UserRoleController.class);
	
	
	@Autowired
	private MesRoleServiceI mesRoleServiceI;
	
	/**
	 * 跳转到角色管理页面
	 * @return
	 */
	@GetMapping(value="/toList")
	public String gotoRoleManage() {
		log.info("角色管理界面");
		return "/user/role";
	}
	
	
	
	/**
	 * 分页查询角色列表
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@GetMapping(value="/list")
	@ResponseBody
	public AjaxReturnVO getRole(@RequestParam("pageNum")Integer pageNum,@RequestParam("pageSize")Integer pageSize) {
		
		log.info("查询角色列表信息");
		
		AjaxReturnVO vo = new AjaxReturnVO();
		
		try {
			vo.setOk(true);
			PageBean pb = this.mesRoleServiceI.getRolesForBean(pageNum,pageSize);
			vo.setData(pb);
		} catch (Exception e) {
			e.printStackTrace();
			vo.setOk(false);
			vo.setMsg("报错了");
		}
		
		return vo;
	}
	
	/**
	 * 添加角色信息
	 * @param vo
	 * @return
	 */
    @PostMapping("/add")
    @ResponseBody
    public AjaxReturnVO add(@RequestBody MesRoleVO vo) {
    	log.info("添加角色配置信息");
		AjaxReturnVO ajaxVo = new AjaxReturnVO();
		try {
			this.mesRoleServiceI.save(vo);
	    	ajaxVo.setData(vo);
	    	ajaxVo.setOk(true);
		} catch (Exception e) {
			e.printStackTrace();
			ajaxVo.setMsg("请求失败");
			ajaxVo.setOk(false);
		}
    	return ajaxVo;
    }

    /**
     * 编辑角色信息
     * @param vo
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxReturnVO edit(@RequestBody MesRoleVO vo) {
    	log.info("修改角色配置信息");
		AjaxReturnVO ajaxVo = new AjaxReturnVO();
		try {
			this.mesRoleServiceI.edit(vo);
	    	ajaxVo.setData(vo);
	    	ajaxVo.setOk(true);
		} catch (Exception e) {
			e.printStackTrace();
			ajaxVo.setMsg("请求失败");
			ajaxVo.setOk(false);
		}
    	return ajaxVo;
    }
    
    /**
     * 删除角色
     * @param id
     * @return
     */
    @GetMapping("/delete")
    @ResponseBody
    public AjaxReturnVO deleteById(@RequestParam("id") String id) {
    	AjaxReturnVO j = new AjaxReturnVO();
    	try {
	    	this.mesRoleServiceI.deleteRoleById(id);
	    	j.setOk(true);
		} catch (Exception e) {
			e.printStackTrace();
			j.setOk(false);
		}
    	return j;
    }
	
	/**
     * 查询用户对应的角色
     * @param id
     * @return
     */
    @GetMapping("/getRoleListByUserId")
    @ResponseBody
    public AjaxReturnVO getRoleListByUserId(@RequestParam("id") String id) {
    	
    	log.info("根据用户ID查询角色列表信息");
		AjaxReturnVO vo = new AjaxReturnVO();
		
		try {
			vo.setOk(true);
			Map<String,Object> map = new HashMap<String,Object>();
			List<MesUserAndRoleVO> left = this.mesRoleServiceI.getRoleList();
			List<MesUserAndRoleVO> right = this.mesRoleServiceI.getRoleListByUserId(id);
			map.put("left", left);
			map.put("right", right);
			map.put("userId", id);
			vo.setAttrs(map);
		} catch (Exception e) {
			e.printStackTrace();
			vo.setOk(false);
			vo.setMsg("查询失败");
		}
		
		return vo;
    	
    }
    
    /**
     * 保存角色用户关系
     * @param roleList
     * @return
     */
    @PostMapping(value="/saveUserAndRole")
    @ResponseBody
    public AjaxReturnVO saveUserAndRole(@RequestBody List<MesUserAndRole> roleList) {
    	
    	AjaxReturnVO vo = new AjaxReturnVO();
    	
    	try {
			log.info("保存用户角色");
    		vo.setOk(true);
    		this.mesRoleServiceI.saveUserAndRole(roleList);
		} catch (Exception e) {
			vo.setOk(false);
			e.printStackTrace();
			vo.setMsg("查询失败");
		}
    	return vo;
    }
    
    @GetMapping(value="/deleteRoleByuserId")
    @ResponseBody
    public AjaxReturnVO deleteRoleByuserId(@RequestParam("userId")String userId) {
    	AjaxReturnVO vo = new AjaxReturnVO();
    	
    	try {
			log.info("删除用户与角色");
    		vo.setOk(true);
    		this.mesRoleServiceI.deleteRoleByuserId(userId);
		} catch (Exception e) {
			vo.setOk(false);
			e.printStackTrace();
			vo.setMsg("删除失败");
		}
    	return vo;
    	
    }
    
    /**
     * 查询角色对应的菜单权限
     * @param id
     * @return
     */
    @GetMapping("/getPermissionByRoleId")
    @ResponseBody
    public AjaxReturnVO getPermissionByRoleId(@RequestParam("id") String id) {
    	
    	log.info("根据角色ID查询菜单列表信息");
		AjaxReturnVO vo = new AjaxReturnVO();
		
		try {
			vo.setOk(true);
			Map<String,Object> map = new HashMap<String,Object>();
			List<MesRoleAndPermissionVO> left = this.mesRoleServiceI.getPermissionList();
			List<MesRoleAndPermissionVO> right = this.mesRoleServiceI.getPermissionByRoleId(id);
			map.put("left", left);
			map.put("right", right);
			map.put("roleId", id);
			vo.setAttrs(map);
		} catch (Exception e) {
			e.printStackTrace();
			vo.setOk(false);
			vo.setMsg("查询失败");
		}
		
		return vo;
    	
    }
    
    /**
     * 保存角色菜单关系
     * @param permissionList
     * @return
     */
    @PostMapping(value="/saveRoleAndPeimission")
    @ResponseBody
    public AjaxReturnVO saveRoleAndPermission(@RequestBody List<MesRoleAndPermission> permissionList) {
    	
    	AjaxReturnVO vo = new AjaxReturnVO();
    	
    	try {
    		vo.setOk(true);
    		this.mesRoleServiceI.saveRoleAndPermission(permissionList);
		} catch (Exception e) {
			vo.setOk(false);
			e.printStackTrace();
			vo.setMsg("查询失败");
		}
    	return vo;
    }
    
    /**
     * 根据角色Id删除作业权限
     */
    @GetMapping(value="/deleteRoleAndPermissionByRoleId")
    @ResponseBody
    public AjaxReturnVO deleteRoleAndPermissionByRoleId(@RequestParam("id")  String id) {
    	
    	AjaxReturnVO vo = new AjaxReturnVO();
    	
    	try {
    		vo.setOk(true);
    		this.mesRoleServiceI.deleteRoleAndPermissionByRoleId(id);
		} catch (Exception e) {
			vo.setOk(false);
			e.printStackTrace();
			vo.setMsg("删除失败");
		}
    	return vo;
    }
    
    /**
     * 保存角色菜单关系
     * @param permissionList
     * @return
     */
    @PostMapping(value="/saveRoleAndJob")
    @ResponseBody
    public AjaxReturnVO saveRoleAndJob(@RequestBody List<MesRoleAndJob> jobList) {
    	
    	AjaxReturnVO vo = new AjaxReturnVO();
    	
    	try {
    		vo.setOk(true);
    		this.mesRoleServiceI.saveRoleAndJob(jobList);
		} catch (Exception e) {
			vo.setOk(false);
			e.printStackTrace();
			vo.setMsg("查询失败");
		}
    	return vo;
    }
    
    /**
     * 根据角色Id删除作业权限
     */
    @GetMapping(value="/deleteRoleAndJobByRoleId")
    @ResponseBody
    public AjaxReturnVO deleteRoleAndJobByRoleId(@RequestParam("id")  String id) {
    	
    	AjaxReturnVO vo = new AjaxReturnVO();
    	
    	try {
    		vo.setOk(true);
    		this.mesRoleServiceI.deleteRoleAndJobByRoleId(id);
		} catch (Exception e) {
			vo.setOk(false);
			e.printStackTrace();
			vo.setMsg("删除失败");
		}
    	return vo;
    }
    
	
}
